<?php

namespace app\index\controller;

use app\index\model\Routers;
use think\Controller;
use think\Facade\Request;
use think\Db;
use think\Facade\Config;
use Units\Http;
use PHPExcel_IOFactory;
use PHPExcel;
class Test extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
           $page = input('get.page','1');
           $startItme = ( $page - 1) *10;
           $customer = Db::table('tb_order')->where(['WareHouse'=>1200])->limit($startItme,10)->select();
           foreach ($customer as $item ) {
               echo "RUNNING";
               echo "<hr/>";
              $latLng = $this->getLatlng($item['Address'].','.$item['City'].','.$item['Region']);
              $update =['lat'=>$latLng['lat'],'lng'=>$latLng['lng']];
              Db::table('tb_order')->where(['id'=>$item['id']])->update($update);
           }
           echo "DONE";
    }

    private function getLatlng($url){
        $url = str_replace(' ','+' ,$url);
        $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$url."&key=".Config::get('GOOGLE_MAP_API_KEY');
        $res = Http::instance()->request( $url , 'post')->data();
        $res = json_decode($res , true);
        if( $res['status']=='OK') {
            return $res['results']['0']['geometry']['location'];
        }else{
            return ['lat'=>'0','lng'=>'0'];
        }
    }

    /**
     * 导入司机数据
     * @return mixed
     */
    public function importdriver(){

        if(Request::isPost()){
           exit('no work');
            $wareHouse = Request::post('warehouse');
            $objPHPExcel = \PHPExcel_IOFactory::load($this->getUploadExcle());
            $objPHPExcel->setActiveSheetIndex(0);
            $routers=[];
            $i=1;
            $start = time();
            while (true){
                $i++;
                $router['driver_id'] = $objPHPExcel->getActiveSheet()->getCell("A{$i}")->getValue();
                $router['nickname'] = $objPHPExcel->getActiveSheet()->getCell("C{$i}")->getValue();
               // $router['mobile_phone'] = $objPHPExcel->getActiveSheet()->getCell("C{$i}")->getValue();
             //   $router['depot'] = $objPHPExcel->getActiveSheet()->getCell("D{$i}")->getValue();
                $router['warehouse'] = 'Milpitas';
              //  $router['time_zone_offset'] = $objPHPExcel->getActiveSheet()->getCell("AD{$i}")->getValue();
              //  $router['address'] = $objPHPExcel->getActiveSheet()->getCell("N{$i}")->getValue();
                $address['city'] = $objPHPExcel->getActiveSheet()->getCell("P{$i}")->getValue();
                $address['region'] = $objPHPExcel->getActiveSheet()->getCell("Q{$i}")->getValue();
                $address['address'] = $objPHPExcel->getActiveSheet()->getCell("N{$i}")->getValue();

                $router['address'] = str_replace(' ','+',$address['address'].','.$address['city'].','.$address['region']);
                $router['start_location'] = $objPHPExcel->getActiveSheet()->getCell("L{$i}")->getValue();
                $router['end_location'] = $objPHPExcel->getActiveSheet()->getCell("M{$i}")->getValue();
              //  $router['depot'] = $objPHPExcel->getActiveSheet()->getCell("D{$i}")->getValue();

                if(!$router['driver_id']) break;
                 Db::table('tb_driver')->insert($router);

            }
            echo "done";
            die;

        }
       return $this->fetch();
    }


    public function importvehicle(){
        if(Request::isPost()){
           DIE;
            $wareHouse = Request::post('warehouse');
            $objPHPExcel = \PHPExcel_IOFactory::load($this->getUploadExcle());
            $objPHPExcel->setActiveSheetIndex(0);
            $routers=[];
            $i=1;
            $start = time();
            while (true){
                $i++;
                $router['vehicle_id'] = $objPHPExcel->getActiveSheet()->getCell("A{$i}")->getValue();
                $router['name'] = $objPHPExcel->getActiveSheet()->getCell("C{$i}")->getValue();
                $router['priority'] = $objPHPExcel->getActiveSheet()->getCell("D{$i}")->getValue();
               // $router['mobile_phone'] = $objPHPExcel->getActiveSheet()->getCell("C{$i}")->getValue();
                $router['depot'] = $objPHPExcel->getActiveSheet()->getCell("E{$i}")->getValue();
                $router['warehouse'] = 'Milpitas';
                $router['max_stops'] = $objPHPExcel->getActiveSheet()->getCell("X{$i}")->getValue();
                //$router['address'] = $objPHPExcel->getActiveSheet()->getCell("N{$i}")->getValue();

                if(!$router['vehicle_id']) break;
                Db::table('tb_vehicle')->insert($router);

            }
            echo "done";
            die;

        }
        return $this->fetch();
    }
    /**获取上传文件
     * @return string
     */
    public function getUploadExcle(){
        var_dump($_FILES);die;
        $file = request()->file('driver');
        $info = $file->move( '../uploads');
        if($info){
            return $uploadFile = "../uploads/".$info->getSaveName();
        }else{
            exit($file->getError());
        }
    }

    //更新司机的经纬度
    public function updatedrivelatandlng(){
        $arr = Db::table('tb_driver')->select();
        foreach ( $arr as $key=> $val ) {
            $latAndLng = $this->getLatlng( $val['address']);
            Db::table('tb_driver')->where(['driver_id'=>$val['driver_id']])->update($latAndLng);
        }
        echo 'SUCCESS';
    }

}
